from pyjvm.instructions.common import Instruction
from pyjvm.runtime.thread import Frame


def _cmp(frame: Frame, flag=False):
    stack = frame.operand_stack
    val2 = stack.pop()
    val1 = stack.pop()
    if val1 > val2:
        return stack.push(1)
    elif val1 == val2:
        return stack.push(0)
    elif val1 < val2:
        return stack.push(-1)
    if flag:
        return stack.push(1)
    stack.push(-1)


class ICMP(Instruction):

    def execute(self, frame: Frame):
        _cmp(frame)


class LCMP(Instruction):

    def execute(self, frame: Frame):
        _cmp(frame)


class FCMPG(Instruction):

    def execute(self, frame: Frame):
        return _cmp(frame, True)


class DCMPG(FCMPG):
    pass


class FCMPL(Instruction):

    def execute(self, frame: Frame):
        return _cmp(frame, False)


class DCMPL(FCMPL):
    pass


__all__ = ['ICMP', 'DCMPG', 'DCMPL', 'FCMPG', 'FCMPL', 'LCMP']
